package com.opencvjava.lessons.imgproc;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.springframework.stereotype.Service;

import static com.opencvjava.support.util.CvUtils.imshow;
import static org.opencv.core.Core.add;
import static org.opencv.core.Core.multiply;
import static org.opencv.imgproc.Imgproc.INTER_NEAREST;
import static org.opencv.imgproc.Imgproc.MORPH_HITMISS;
import static org.opencv.imgproc.Imgproc.morphologyEx;
import static org.opencv.imgproc.Imgproc.resize;

@Service
public class L6_HitOrMiss {
    public void test() {
        Mat input_image = new Mat( 8, 8, CvType.CV_8UC1 );
        int row = 0, col = 0;
        input_image.put(row ,col,
                0, 0, 0, 0, 0, 0, 0, 0,
                0, 255, 255, 255, 0, 0, 0, 255,
                0, 255, 255, 255, 0, 0, 0, 0,
                0, 255, 255, 255, 0, 255, 0, 0,
                0, 0, 255, 0, 0, 0, 0, 0,
                0, 0, 255, 0, 0, 255, 255, 0,
                0, 255, 0, 255, 0, 0, 255, 0,
                0, 255, 255, 255, 0, 0, 0, 0);
        Mat kernel = new Mat( 3, 3, CvType.CV_16S );
        kernel.put(row ,col,
                0, 1, 0,
                1, -1, 1,
                0, 1, 0 );
        Mat output_image = new Mat();
        morphologyEx(input_image, output_image, MORPH_HITMISS, kernel);
        int rate = 50;
        add(kernel, new Scalar(1), kernel);
        multiply(kernel, new Scalar(127), kernel);
        kernel.convertTo(kernel, CvType.CV_8U);
        resize(kernel, kernel, new Size(), rate, rate, INTER_NEAREST);
        imshow("kernel", kernel);
        resize(input_image, input_image, new Size(), rate, rate, INTER_NEAREST);
        imshow("Original", input_image);
        resize(output_image, output_image, new Size(), rate, rate, INTER_NEAREST);
        imshow("Hit or Miss", output_image);
    }
}
